//
//  YTStashRecordsOutListViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/11/14.
//

#import "YTStashRecordsOutListViewController.h"

#import "ExamineViewController.h"
#import "YTStashRecordModel.h"
#import "YTStashScreenModel.h"
#import "TimeFrameShowView.h"
#import "YTStashRecordsDetailsViewController.h"
#import "YTStashRecordsListTableViewCell.h"
#import "YTStashRecordsScreenViewController.h"
#import "YTStashRecordsResultViewController.h"

@interface YTStashRecordsOutListViewController ()

@property (nonatomic ,strong)  UIView * btnline;

@property (nonatomic ,strong) UIButton * SXBtn;

@property (nonatomic ,strong) NSMutableArray <YTStashRecordModel *> * modelArray;

@property (nonatomic ,strong) YTStashScreenModel * model;

///获取数量
@property (nonatomic ,strong) NSURLSessionDataTask * task;


@end

@implementation YTStashRecordsOutListViewController

-(NSMutableArray<YTStashRecordModel *> *)modelArray{
    if(!_modelArray){
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

- (YTStashScreenModel *)model{
    if(!_model){
        _model = [YTStashScreenModel new];
        _model.type = @"1";
    }
    return _model;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self refreshingData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.shutDownBack = YES;
    self.isTableviewZD = YES;
    self.view.backgroundColor = KcolorHex(@"#FBFBFB");
    
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    
  
    
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(120));
    }];
    
    
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    
    
    UILabel * leftlab = [DTKTool labelWithText:@"出库记录" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    
    UIView * ssview = [UIView new];
    ssview.backgroundColor = KcolorHex(@"#F1F5FA");
    [self.view addSubview:ssview];
    [ssview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.height.offset(kwt(48));
        make.top.equalTo(topview.mas_bottom);
    }];
    UIButton * jjSSBtn = [UIButton buttonWithType:0];
    jjSSBtn.addimg([UIImage imageNamed:@"seach"]).addTarget(self,@selector(jjssBtnclick));
    [topview addSubview:jjSSBtn];
    [jjSSBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.centerY.equalTo(leftlab);
        make.width.height.offset(kwt(30));
    }];
 
   
   
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#EBEDF0");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.top.equalTo(topview.mas_bottom);
    }];
    [self addFooterView:^{
        [weakSelf addMoreData];
    }];
    [self addHeaderView:^{
        [weakSelf refreshingData];
    }];
}
- (void)jjssBtnclick{
    YTStashRecordsResultViewController * vc = [YTStashRecordsResultViewController new];
    YTStashScreenModel * model = [YTStashScreenModel new];
    model.type = @"1";
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)refreshingData{
    self.pageNo = 1;
    [self.modelArray removeAllObjects];
    [self loadNewData];

}


- (void)SXBtnClick{
    YTStashRecordsScreenViewController * vc = [YTStashRecordsScreenViewController new];
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)loadNewData {
    if(self.task){
        [self.task cancel];
    }
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [self.model CRKSXDict];
    [dict setObject:@(self.pageNo) forKey:@"pageNo"];
    [dict setObject:@(self.pageSize) forKey:@"pageSize"];
    if(self.modelArray.count > 0){
        [dict setObject:self.modelArray.lastObject.createTime forKey:@"createTime"];
    }

    self.task = [SCNetManger warehouseBoundListWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        self.task = nil;
        if(model.code == 200){
            NSArray * array = [NSArray yy_modelArrayWithClass:YTStashRecordModel.class json:model.result[@"records"]];
            [self.modelArray addObjectsFromArray:array];
            if(array.count == 0 && self.modelArray.count > 0){
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableview.mj_footer endRefreshing];
            }
            [self.tableview.mj_header endRefreshing];
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}


//- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
//    return kwt(100);
//}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * headerview = [UIView new];
    headerview.backgroundColor = KcolorHex(@"#EBEDF0");
    UIView * btnview = [UIView new];
    MJWeakSelf
    btnview.addClickBlock(^{
        [weakSelf openTime];
    });
    [headerview addSubview:btnview];
    btnview.backgroundColor= UIColor.whiteColor;
    [DTKTool YJ:kwt(4) andView:btnview];
    [btnview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(kwt(10));
        make.height.offset(kwt(31));
    }];
    UILabel * time = [DTKTool labelWithText:@"-" andFont:kFont(16) andTextColor:KcolorHex(@"#222222")];
    [btnview addSubview:time];
    [time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(kwt(11));
    }];
    
    UIImageView * img = kimageView(@"arrow_down");
    [btnview addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(kwt(-11));
        make.width.offset(kwt(12));
        make.height.offset(kwt(7));
        make.left.equalTo(time.mas_right).offset(kwt(11));
    }];
    if(self.model.createStartTime.length > 0){
        if([self.model.createStartTime isEqualToString:self.model.createEndTime]){
            time.text = self.model.createStartTime;
        } else {
            time.text = [NSString stringWithFormat:@"%@~%@",self.model.createStartTime,self.model.createEndTime];
        }
    } else {
        time.text = self.model.monthTime;
    }
    
    UIButton * SXBtn = [UIButton buttonWithType:0];
    SXBtn.addTarget(self,@selector(SXBtnClick));
    SXBtn.addNormalText(@"筛选").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(17));
    [SXBtn setTitleColor:KcolorHex(@"#0967FF") forState:UIControlStateSelected];
    [SXBtn setImage:[UIImage imageNamed:@"CRscreen"] forState:UIControlStateNormal];
    [SXBtn setImage:[UIImage imageNamed:@"CRscreen2"] forState:UIControlStateSelected];
    [SXBtn setImageEdgeInsets:UIEdgeInsetsMake(0, kwt(50), 0, 0)];
    [SXBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, kwt(-40), 0, 0)];
    [headerview addSubview:SXBtn];
    SXBtn.selected = self.model.isNew;
    [SXBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(kwt(-10));
        make.height.offset(kwt(40));
        make.width.offset(kwt(80));
    }];
  
    
    
    return headerview;
    
}


- (void)openTime{
    MJWeakSelf
    if(self.model.createEndTime.length > 0){
        [TimeFrameShowView showviewWithTitle:@"请选择时间" andStartTime:self.model.createStartTime andEndTime:self.model.createEndTime andRightBlock:^(NSString * _Nonnull StartTime, NSString * _Nonnull EndTime) {
            weakSelf.model.createEndTime = EndTime;
            weakSelf.model.createStartTime = StartTime;
            [weakSelf refreshingData];
        } andCZBlock:^{
            weakSelf.model.createStartTime = @"";
            weakSelf.model.createEndTime = @"";
            weakSelf.SXBtn.selected = weakSelf.model.isNew;
            [weakSelf refreshingData];
        }];
    } else{
        
        [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYM title:@"请选择时间" selectValue:self.model.monthTime minDate:nil maxDate:nil isAutoSelect:NO resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
            weakSelf.model.monthTime = selectValue;
            [weakSelf refreshingData];
        }];
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kwt(50);
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.modelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return self.modelArray[indexPath.row].cellH;
    return kwt(210);
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    
    YTStashRecordsListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YTStashRecordsListTableViewCell"];
    if (cell == nil) {
        cell = [[YTStashRecordsListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YTStashRecordsListTableViewCell"];
    }
    if(self.modelArray.count > indexPath.row){
        cell.model = self.modelArray[indexPath.row];

    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell ;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YTStashRecordsDetailsViewController * vc = [YTStashRecordsDetailsViewController new];
    vc.ID = self.modelArray[indexPath.row].ID;
    [self.navigationController pushViewController:vc animated:YES];
}
@end

